package com.igoal.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

import com.igoal.dto.NewsDTO;
import com.igoal.dto.UserDTO;
import com.igoal.service.MessageManager;
import com.igoal.service.NewsManager;
import com.igoal.service.TagService;
import com.igoal.service.UserManagerExt;
import com.igoal.utils.ConvertUtils;
import com.igoal.web.model.BaseModel;
import com.igoal.web.model.NewsModel;

@Controller
@RequestMapping("/news.html")
public class NewsController extends BaseController {
	private NewsManager newsManager;
	private TagService tagService;
	
	@Autowired
	public void setTagService(TagService tagService) {
		this.tagService = tagService;
	}

	@Autowired
	public void setNewsManager(NewsManager newsManager) {
		this.newsManager = newsManager;
	}
	
	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}
	
	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}
	
	public String getViewPrefix() {
		return BaseControllerDelegate.DEFAULT_VIEW_PREFIX;
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public String showNews(ModelMap modelMap, HttpServletRequest request){				
		NewsModel model = new NewsModel();
		
		initTopPage(model, request);
//		initLeftPage(model);
		initRightPage(model, request);
		
		initModel(model, request);
		
		applyModel(modelMap, model);
		
		return getViewPrefix() + "news";
	}
	
	public void initModel(BaseModel baseModel, HttpServletRequest request){
		NewsModel model = (NewsModel) baseModel;
		List<NewsDTO> news = null;		
		String tagName = request.getParameter("tagName");
		Long tagId = ConvertUtils.toLong(request.getParameter("tagId"), -1L);
		if(tagName == null && tagId == -1L){
			news = newsManager.listNews(null, false);
		}else if(tagId > 0L){
			news = tagService.listTaggedNews(tagId, null, null, false);
		}else if(!StringUtils.isBlank(tagName)){
			news = tagService.listTaggedNews(null, tagName, null, false);
		}
		
		if(news == null)
			news = new ArrayList<NewsDTO>();
		
		model.setNews(news);
		model.setRandomNews(newsManager.getRandomNews());
	}

	public void initTopPage(BaseModel model, HttpServletRequest request) {
		UserDTO sessionUser = null;
		if(isLoginUser())
			sessionUser = getUserSession();
		initTopZone(sessionUser, model);
	}

	public void initRightPage(BaseModel model, HttpServletRequest request) {
		UserDTO sessionUser = null;
		if(isLoginUser())
			sessionUser = getUserSession();
		initRightZone(sessionUser, model);
	}

	public void initLeftPage(BaseModel baseModel, HttpServletRequest request) {}

}
